package com.fastloan.mgt.customer.service.impl;

import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.customer.dao.DefCardInfoMapper;
import com.fastloan.mgt.customer.dao.ZyCustomerCardMapper;
import com.fastloan.mgt.customer.enums.CustomerEnums;
import com.fastloan.mgt.customer.model.DefCardInfo;
import com.fastloan.mgt.customer.model.KXBankCardResult;
import com.fastloan.mgt.customer.model.ZyCustomer;
import com.fastloan.mgt.customer.model.ZyCustomerCard;
import com.fastloan.mgt.customer.service.ZyCustomerCardService;
import com.fastloan.mgt.customer.service.ZyCustomerService;
import com.fastloan.mgt.verify.model.VerifyResult;
import com.fastloan.mgt.verify.model.bankcard.BankCardVerifyParam;
import com.fastloan.mgt.verify.model.bankcard.BankCardVerifyResult;
import com.fastloan.mgt.verify.service.VerifyService;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author yanwe
 * createTime 2017-12-2017/12/8 16:19
 */
@Service
public class ZyCustomerCardServiceImpl extends BasicServiceImpl<ZyCustomerCard> implements ZyCustomerCardService {

    @Autowired
    private ZyCustomerService customerService;

    @Autowired
    private ZyCustomerCardMapper customerCardMapper;

    @Autowired
    private DefCardInfoMapper defCardInfoMapper;

    @Autowired
    public VerifyService verifyService;

    @Override
    public ResultTo bindCard(String cardNum, String phone, String customerId){
        ZyCustomer customer = customerService.selectByPrimaryKey(customerId);
        if (CustomerEnums.VerifyEnum.NO_VERIFY.getCode().equals(customer.getVerify())){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"该客户未通过验证无法绑定银行卡!");
        }
        BankCardVerifyResult bankCardVerifyResult = verifyService
                .verifyBankCard(BankCardVerifyParam.BankCardVerifyParamBuilder.aBankCardVerifyParam()
                        .withBankCardNo(cardNum).withIdentity(customer.getCardNum()).withName(customer.getCname())
                        .withPhone(phone).build());
        if (!bankCardVerifyResult.allAccord()){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"验证未通过!");
        }
        ZyCustomerCard customerCard = new ZyCustomerCard(customerId, cardNum);
        if (customerCardMapper.selectCount(customerCard) != 0){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"该银行卡已绑定!");
        }
        customerCardMapper.insert(customerCard);
        saveBankCardType(cardNum);
        return new ResultTo();
    }

    @Override
    public List<ZyCustomerCard> getCustomerCard(String customerId){
        return customerCardMapper.selectCustomerCard(customerId);
    }

    /**
     * 保存银行卡类型
     *
     * @param cardNum 银行卡号
     */
    private void saveBankCardType(String cardNum) {
        DefCardInfo criteria = new DefCardInfo();
        criteria.setBin(cardNum.substring(0,6));
        int count = defCardInfoMapper.selectCount(criteria);
        if (count != 0 ){//已存在类型则不需要保存
            return;
        }
        //调用快信接口获取银行卡类型
        KXBankCardResult kxBankCardResult = verifyService.selectBankCardType(cardNum);
        DefCardInfo defCardInfo = new DefCardInfo();
        defCardInfo.setBin(kxBankCardResult.getBin());
        defCardInfo.setCardName(kxBankCardResult.getCardName());
        defCardInfo.setCardType(kxBankCardResult.getCardType());
        defCardInfo.setIssuer(kxBankCardResult.getBankName());
        defCardInfo.setIssuerCode(kxBankCardResult.getBankId());
        defCardInfoMapper.insert(defCardInfo);
    }
}
